package org.zachary.agent.infrastructure.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.zachary.agent.domain.aggregate_model.user.ProfilePicture;
import org.zachary.agent.domain.aggregate_model.user.User;
import org.zachary.agent.domain.aggregate_model.user.Username;

import java.util.List;
import java.util.Map;

public interface SpringDataUserRepository extends JpaRepository<User, String> {

    User findByUsername(@Param("username") Username username);


    User queryByUuid(String uuid);

    @Modifying(clearAutomatically = true)
    @Query("UPDATE User u SET " +
            "u.username.username=:#{#user.username}," +
            "u.nickName.nickName=:#{#user.nickName}," +
            "u.profilePicture.profilePicture=:#{#user.profilePicture}," +
            "u.contactUs.phone=:#{#user.phone}," +
            "u.contactUs.email=:#{#user.email}," +
            "u.gender.gender=:#{#user.gender}," +
            "u.address.province=:#{#user.province}," +
            "u.address.city=:#{#user.city}," +
            "u.address.county=:#{#user.county}," +
            "u.address.township=:#{#user.township}, " +
            "u.createTime=CURRENT_TIMESTAMP ,"+
            "u.updateTime=CURRENT_TIMESTAMP ," +
            "u.updated=true "+
            "WHERE u.uuid= :uuid")
    void updateUserByUuid(@Param("uuid") String uuid,@Param("user") User user);

    @Query("SELECT u FROM User u WHERE u.id = :id")
    User findByIntegerId(@Param("id") Integer id);
    @Modifying
    @Query("UPDATE User u SET " +
            "u.profilePicture=:#{#url} "+
            "WHERE u.id=:userId")
    void updateUserAvatar(@Param("userId") Integer userId,@Param("url") ProfilePicture url);



    @Query("SELECT NEW org.zachary.agent.infrastructure.repository.UserIdCountryProjection(u.id, u.address.county,u.profilePicture.profilePicture) " +
            "FROM User u WHERE u.id IN :ids")
    List<UserIdCountryProjection> loadCountiesByIds(@Param("ids") List<Integer> ids);

}
